﻿using System;
using System.Threading;
using UniRx.Operators;

namespace UniRx
{
	// Token: 0x0200022E RID: 558
	public static class Observer
	{
		// Token: 0x06000D12 RID: 3346 RVA: 0x0003F18A File Offset: 0x0003D58A
		internal static IObserver<T> CreateSubscribeObserver<T>(Action<T> onNext, Action<Exception> onError, Action onCompleted)
		{
			if (onNext == Stubs<T>.Ignore)
			{
				return new Observer.Subscribe_<T>(onError, onCompleted);
			}
			return new Observer.Subscribe<T>(onNext, onError, onCompleted);
		}

		// Token: 0x06000D13 RID: 3347 RVA: 0x0003F1AC File Offset: 0x0003D5AC
		internal static IObserver<T> CreateSubscribeWithStateObserver<T, TState>(TState state, Action<T, TState> onNext, Action<Exception, TState> onError, Action<TState> onCompleted)
		{
			return new Observer.Subscribe<T, TState>(state, onNext, onError, onCompleted);
		}

		// Token: 0x06000D14 RID: 3348 RVA: 0x0003F1B7 File Offset: 0x0003D5B7
		internal static IObserver<T> CreateSubscribeWithState2Observer<T, TState1, TState2>(TState1 state1, TState2 state2, Action<T, TState1, TState2> onNext, Action<Exception, TState1, TState2> onError, Action<TState1, TState2> onCompleted)
		{
			return new Observer.Subscribe<T, TState1, TState2>(state1, state2, onNext, onError, onCompleted);
		}

		// Token: 0x06000D15 RID: 3349 RVA: 0x0003F1C4 File Offset: 0x0003D5C4
		internal static IObserver<T> CreateSubscribeWithState3Observer<T, TState1, TState2, TState3>(TState1 state1, TState2 state2, TState3 state3, Action<T, TState1, TState2, TState3> onNext, Action<Exception, TState1, TState2, TState3> onError, Action<TState1, TState2, TState3> onCompleted)
		{
			return new Observer.Subscribe<T, TState1, TState2, TState3>(state1, state2, state3, onNext, onError, onCompleted);
		}

		// Token: 0x06000D16 RID: 3350 RVA: 0x0003F1D3 File Offset: 0x0003D5D3
		public static IObserver<T> Create<T>(Action<T> onNext)
		{
			return Observer.Create<T>(onNext, Stubs.Throw, Stubs.Nop);
		}

		// Token: 0x06000D17 RID: 3351 RVA: 0x0003F1E5 File Offset: 0x0003D5E5
		public static IObserver<T> Create<T>(Action<T> onNext, Action<Exception> onError)
		{
			return Observer.Create<T>(onNext, onError, Stubs.Nop);
		}

		// Token: 0x06000D18 RID: 3352 RVA: 0x0003F1F3 File Offset: 0x0003D5F3
		public static IObserver<T> Create<T>(Action<T> onNext, Action onCompleted)
		{
			return Observer.Create<T>(onNext, Stubs.Throw, onCompleted);
		}

		// Token: 0x06000D19 RID: 3353 RVA: 0x0003F201 File Offset: 0x0003D601
		public static IObserver<T> Create<T>(Action<T> onNext, Action<Exception> onError, Action onCompleted)
		{
			if (onNext == Stubs<T>.Ignore)
			{
				return new Observer.EmptyOnNextAnonymousObserver<T>(onError, onCompleted);
			}
			return new Observer.AnonymousObserver<T>(onNext, onError, onCompleted);
		}

		// Token: 0x06000D1A RID: 3354 RVA: 0x0003F223 File Offset: 0x0003D623
		public static IObserver<T> CreateAutoDetachObserver<T>(IObserver<T> observer, IDisposable disposable)
		{
			return new Observer.AutoDetachObserver<T>(observer, disposable);
		}

		// Token: 0x0200022F RID: 559
		private class AnonymousObserver<T> : IObserver<T>
		{
			// Token: 0x06000D1B RID: 3355 RVA: 0x0003F22C File Offset: 0x0003D62C
			public AnonymousObserver(Action<T> onNext, Action<Exception> onError, Action onCompleted)
			{
				this.onNext = onNext;
				this.onError = onError;
				this.onCompleted = onCompleted;
			}

			// Token: 0x06000D1C RID: 3356 RVA: 0x0003F249 File Offset: 0x0003D649
			public void OnNext(T value)
			{
				if (this.isStopped == 0)
				{
					this.onNext(value);
				}
			}

			// Token: 0x06000D1D RID: 3357 RVA: 0x0003F262 File Offset: 0x0003D662
			public void OnError(Exception error)
			{
				if (Interlocked.Increment(ref this.isStopped) == 1)
				{
					this.onError(error);
				}
			}

			// Token: 0x06000D1E RID: 3358 RVA: 0x0003F281 File Offset: 0x0003D681
			public void OnCompleted()
			{
				if (Interlocked.Increment(ref this.isStopped) == 1)
				{
					this.onCompleted();
				}
			}

			// Token: 0x0400075D RID: 1885
			private readonly Action<T> onNext;

			// Token: 0x0400075E RID: 1886
			private readonly Action<Exception> onError;

			// Token: 0x0400075F RID: 1887
			private readonly Action onCompleted;

			// Token: 0x04000760 RID: 1888
			private int isStopped;
		}

		// Token: 0x02000230 RID: 560
		private class EmptyOnNextAnonymousObserver<T> : IObserver<T>
		{
			// Token: 0x06000D1F RID: 3359 RVA: 0x0003F29F File Offset: 0x0003D69F
			public EmptyOnNextAnonymousObserver(Action<Exception> onError, Action onCompleted)
			{
				this.onError = onError;
				this.onCompleted = onCompleted;
			}

			// Token: 0x06000D20 RID: 3360 RVA: 0x0003F2B5 File Offset: 0x0003D6B5
			public void OnNext(T value)
			{
			}

			// Token: 0x06000D21 RID: 3361 RVA: 0x0003F2B7 File Offset: 0x0003D6B7
			public void OnError(Exception error)
			{
				if (Interlocked.Increment(ref this.isStopped) == 1)
				{
					this.onError(error);
				}
			}

			// Token: 0x06000D22 RID: 3362 RVA: 0x0003F2D6 File Offset: 0x0003D6D6
			public void OnCompleted()
			{
				if (Interlocked.Increment(ref this.isStopped) == 1)
				{
					this.onCompleted();
				}
			}

			// Token: 0x04000761 RID: 1889
			private readonly Action<Exception> onError;

			// Token: 0x04000762 RID: 1890
			private readonly Action onCompleted;

			// Token: 0x04000763 RID: 1891
			private int isStopped;
		}

		// Token: 0x02000231 RID: 561
		private class Subscribe<T> : IObserver<T>
		{
			// Token: 0x06000D23 RID: 3363 RVA: 0x0003F2F4 File Offset: 0x0003D6F4
			public Subscribe(Action<T> onNext, Action<Exception> onError, Action onCompleted)
			{
				this.onNext = onNext;
				this.onError = onError;
				this.onCompleted = onCompleted;
			}

			// Token: 0x06000D24 RID: 3364 RVA: 0x0003F311 File Offset: 0x0003D711
			public void OnNext(T value)
			{
				if (this.isStopped == 0)
				{
					this.onNext(value);
				}
			}

			// Token: 0x06000D25 RID: 3365 RVA: 0x0003F32A File Offset: 0x0003D72A
			public void OnError(Exception error)
			{
				if (Interlocked.Increment(ref this.isStopped) == 1)
				{
					this.onError(error);
				}
			}

			// Token: 0x06000D26 RID: 3366 RVA: 0x0003F349 File Offset: 0x0003D749
			public void OnCompleted()
			{
				if (Interlocked.Increment(ref this.isStopped) == 1)
				{
					this.onCompleted();
				}
			}

			// Token: 0x04000764 RID: 1892
			private readonly Action<T> onNext;

			// Token: 0x04000765 RID: 1893
			private readonly Action<Exception> onError;

			// Token: 0x04000766 RID: 1894
			private readonly Action onCompleted;

			// Token: 0x04000767 RID: 1895
			private int isStopped;
		}

		// Token: 0x02000232 RID: 562
		private class Subscribe_<T> : IObserver<T>
		{
			// Token: 0x06000D27 RID: 3367 RVA: 0x0003F367 File Offset: 0x0003D767
			public Subscribe_(Action<Exception> onError, Action onCompleted)
			{
				this.onError = onError;
				this.onCompleted = onCompleted;
			}

			// Token: 0x06000D28 RID: 3368 RVA: 0x0003F37D File Offset: 0x0003D77D
			public void OnNext(T value)
			{
			}

			// Token: 0x06000D29 RID: 3369 RVA: 0x0003F37F File Offset: 0x0003D77F
			public void OnError(Exception error)
			{
				if (Interlocked.Increment(ref this.isStopped) == 1)
				{
					this.onError(error);
				}
			}

			// Token: 0x06000D2A RID: 3370 RVA: 0x0003F39E File Offset: 0x0003D79E
			public void OnCompleted()
			{
				if (Interlocked.Increment(ref this.isStopped) == 1)
				{
					this.onCompleted();
				}
			}

			// Token: 0x04000768 RID: 1896
			private readonly Action<Exception> onError;

			// Token: 0x04000769 RID: 1897
			private readonly Action onCompleted;

			// Token: 0x0400076A RID: 1898
			private int isStopped;
		}

		// Token: 0x02000233 RID: 563
		private class Subscribe<T, TState> : IObserver<T>
		{
			// Token: 0x06000D2B RID: 3371 RVA: 0x0003F3BC File Offset: 0x0003D7BC
			public Subscribe(TState state, Action<T, TState> onNext, Action<Exception, TState> onError, Action<TState> onCompleted)
			{
				this.state = state;
				this.onNext = onNext;
				this.onError = onError;
				this.onCompleted = onCompleted;
			}

			// Token: 0x06000D2C RID: 3372 RVA: 0x0003F3E1 File Offset: 0x0003D7E1
			public void OnNext(T value)
			{
				if (this.isStopped == 0)
				{
					this.onNext(value, this.state);
				}
			}

			// Token: 0x06000D2D RID: 3373 RVA: 0x0003F400 File Offset: 0x0003D800
			public void OnError(Exception error)
			{
				if (Interlocked.Increment(ref this.isStopped) == 1)
				{
					this.onError(error, this.state);
				}
			}

			// Token: 0x06000D2E RID: 3374 RVA: 0x0003F425 File Offset: 0x0003D825
			public void OnCompleted()
			{
				if (Interlocked.Increment(ref this.isStopped) == 1)
				{
					this.onCompleted(this.state);
				}
			}

			// Token: 0x0400076B RID: 1899
			private readonly TState state;

			// Token: 0x0400076C RID: 1900
			private readonly Action<T, TState> onNext;

			// Token: 0x0400076D RID: 1901
			private readonly Action<Exception, TState> onError;

			// Token: 0x0400076E RID: 1902
			private readonly Action<TState> onCompleted;

			// Token: 0x0400076F RID: 1903
			private int isStopped;
		}

		// Token: 0x02000234 RID: 564
		private class Subscribe<T, TState1, TState2> : IObserver<T>
		{
			// Token: 0x06000D2F RID: 3375 RVA: 0x0003F449 File Offset: 0x0003D849
			public Subscribe(TState1 state1, TState2 state2, Action<T, TState1, TState2> onNext, Action<Exception, TState1, TState2> onError, Action<TState1, TState2> onCompleted)
			{
				this.state1 = state1;
				this.state2 = state2;
				this.onNext = onNext;
				this.onError = onError;
				this.onCompleted = onCompleted;
			}

			// Token: 0x06000D30 RID: 3376 RVA: 0x0003F476 File Offset: 0x0003D876
			public void OnNext(T value)
			{
				if (this.isStopped == 0)
				{
					this.onNext(value, this.state1, this.state2);
				}
			}

			// Token: 0x06000D31 RID: 3377 RVA: 0x0003F49B File Offset: 0x0003D89B
			public void OnError(Exception error)
			{
				if (Interlocked.Increment(ref this.isStopped) == 1)
				{
					this.onError(error, this.state1, this.state2);
				}
			}

			// Token: 0x06000D32 RID: 3378 RVA: 0x0003F4C6 File Offset: 0x0003D8C6
			public void OnCompleted()
			{
				if (Interlocked.Increment(ref this.isStopped) == 1)
				{
					this.onCompleted(this.state1, this.state2);
				}
			}

			// Token: 0x04000770 RID: 1904
			private readonly TState1 state1;

			// Token: 0x04000771 RID: 1905
			private readonly TState2 state2;

			// Token: 0x04000772 RID: 1906
			private readonly Action<T, TState1, TState2> onNext;

			// Token: 0x04000773 RID: 1907
			private readonly Action<Exception, TState1, TState2> onError;

			// Token: 0x04000774 RID: 1908
			private readonly Action<TState1, TState2> onCompleted;

			// Token: 0x04000775 RID: 1909
			private int isStopped;
		}

		// Token: 0x02000235 RID: 565
		private class Subscribe<T, TState1, TState2, TState3> : IObserver<T>
		{
			// Token: 0x06000D33 RID: 3379 RVA: 0x0003F4F0 File Offset: 0x0003D8F0
			public Subscribe(TState1 state1, TState2 state2, TState3 state3, Action<T, TState1, TState2, TState3> onNext, Action<Exception, TState1, TState2, TState3> onError, Action<TState1, TState2, TState3> onCompleted)
			{
				this.state1 = state1;
				this.state2 = state2;
				this.state3 = state3;
				this.onNext = onNext;
				this.onError = onError;
				this.onCompleted = onCompleted;
			}

			// Token: 0x06000D34 RID: 3380 RVA: 0x0003F525 File Offset: 0x0003D925
			public void OnNext(T value)
			{
				if (this.isStopped == 0)
				{
					this.onNext(value, this.state1, this.state2, this.state3);
				}
			}

			// Token: 0x06000D35 RID: 3381 RVA: 0x0003F550 File Offset: 0x0003D950
			public void OnError(Exception error)
			{
				if (Interlocked.Increment(ref this.isStopped) == 1)
				{
					this.onError(error, this.state1, this.state2, this.state3);
				}
			}

			// Token: 0x06000D36 RID: 3382 RVA: 0x0003F581 File Offset: 0x0003D981
			public void OnCompleted()
			{
				if (Interlocked.Increment(ref this.isStopped) == 1)
				{
					this.onCompleted(this.state1, this.state2, this.state3);
				}
			}

			// Token: 0x04000776 RID: 1910
			private readonly TState1 state1;

			// Token: 0x04000777 RID: 1911
			private readonly TState2 state2;

			// Token: 0x04000778 RID: 1912
			private readonly TState3 state3;

			// Token: 0x04000779 RID: 1913
			private readonly Action<T, TState1, TState2, TState3> onNext;

			// Token: 0x0400077A RID: 1914
			private readonly Action<Exception, TState1, TState2, TState3> onError;

			// Token: 0x0400077B RID: 1915
			private readonly Action<TState1, TState2, TState3> onCompleted;

			// Token: 0x0400077C RID: 1916
			private int isStopped;
		}

		// Token: 0x02000236 RID: 566
		private class AutoDetachObserver<T> : OperatorObserverBase<T, T>
		{
			// Token: 0x06000D37 RID: 3383 RVA: 0x0003F5FF File Offset: 0x0003D9FF
			public AutoDetachObserver(IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
			}

			// Token: 0x06000D38 RID: 3384 RVA: 0x0003F60C File Offset: 0x0003DA0C
			public override void OnNext(T value)
			{
				try
				{
					this.observer.OnNext(value);
				}
				catch
				{
					base.Dispose();
					throw;
				}
			}

			// Token: 0x06000D39 RID: 3385 RVA: 0x0003F648 File Offset: 0x0003DA48
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06000D3A RID: 3386 RVA: 0x0003F680 File Offset: 0x0003DA80
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}
		}
	}
}
